require "import"
import "java.util.Date"
import "java.util.Calendar"
import "android.widget.Toast"
import "android.os.SystemClock"
import "android.content.Context"
import "java.text.SimpleDateFormat"
import "java.util.Locale"

local PREFS_NAME, context = "HijriDateAdjustmentPrefs", service
if not lastClick then lastClick = 0 end

local function getPref(key, defaultValue)
  if not context then return defaultValue end
  return context.getSharedPreferences(PREFS_NAME, 0).getInt(key, defaultValue)
end

local function savePref(key, value)
  if context then 
    context.getSharedPreferences(PREFS_NAME, 0).edit().putInt(key, value).apply()
  end
end

local options = {
  {0, "الوضع العادي"}, {1, "زيادة يوم واحد"}, {2, "زيادة يومين"}, {3, "زيادة ٣ أيام"},
  {-1, "إنقاص يوم واحد"}, {-2, "إنقاص يومين"}, {-3, "إنقاص ٣ أيام"}
}
local valueMap = { [0]=1, [1]=2, [2]=3, [3]=4, [-1]=5, [-2]=6, [-3]=7 }

local adjustment = getPref("hijri_day_adjustment", 0)

function cycleSettings()
  local currentIndex = valueMap[adjustment] or 1
  local nextIndex = (currentIndex % #options) + 1
  adjustment = options[nextIndex][1]
  savePref("hijri_day_adjustment", adjustment)
  
  local message = "تعديل الهجري: " .. options[nextIndex][2]
  if service then service.speak(message) end
  Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
end

local function getBatteryLevel()
  if context then
    local bm = context.getSystemService(Context.BATTERY_SERVICE)
    if bm then return bm.getIntProperty(4) end
  end
  return 50
end

function speakTimeAndBattery()
  local now = Date()
  local currentLocale = Locale.getDefault()
  local language = currentLocale.getLanguage()
  local battery = getBatteryLevel()
  local msg = ""
  
  if language == "ar" then
    local hour24 = tonumber(SimpleDateFormat("H", Locale("en")).format(now)) or 0
    local hour = SimpleDateFormat("h", currentLocale).format(now)
    local minute = SimpleDateFormat("m", currentLocale).format(now)
    local second = SimpleDateFormat("s", currentLocale).format(now)
    
    local amPmArabic = "صباحاً"
    if hour24 >= 12 then
      amPmArabic = (hour24 < 18) and "زوالاً" or "مساءً"
    end
    
    msg = string.format("الساعة الآن %s %s، %s دقيقة، %s ثانية. مستوى البطارية %d في المئة.", hour, amPmArabic, minute, second, battery)
    
  elseif language == "fr" then
    local hour = SimpleDateFormat("H", currentLocale).format(now)
    local minute = SimpleDateFormat("m", currentLocale).format(now)
    local second = SimpleDateFormat("s", currentLocale).format(now)
    msg = string.format("Il est %s heures, %s minutes et %s secondes. Le niveau de la batterie est de %d pour cent.", hour, minute, second, battery)
    
  else
    local hour = SimpleDateFormat("h", currentLocale).format(now)
    local minute = SimpleDateFormat("m", currentLocale).format(now)
    local second = SimpleDateFormat("s", currentLocale).format(now)
    local amPm = SimpleDateFormat("a", currentLocale).format(now)
    msg = string.format("The time is %s, %s minutes and %s seconds %s. The battery level is %d percent.", hour, minute, second, amPm, battery)
  end
  
  if service then service.speak(msg) end
end

local now_time = SystemClock.elapsedRealtime()
if now_time - lastClick < 1000 then
  cycleSettings() 
  lastClick = 0
else
  speakTimeAndBattery()
  lastClick = now_time
end
